//
//  HXFileTableViewController.m
//  KD
//
//  Created by ifly on 16/5/13.
//  Copyright © 2016年 Whx. All rights reserved.
//

#import "HXFileTableViewController.h"
#import "HXFileTableViewCell.h"
#import "HXFileDetailsViewController.h"

@interface HXFileTableViewController ()<UISearchControllerDelegate,UISearchBarDelegate>
@property(nonatomic,assign) NSInteger page;
@property(nonatomic,strong) NSMutableArray * dataArray;
@property(nonatomic,strong) UISearchController * searchCon;
@end

@implementation HXFileTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableViewRefresh];
}

-(void)setupTableViewRefresh{
    self.page = 0;
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self.tableView registerNib:[UINib nibWithNibName:@"HXFileTableViewCell" bundle:nil] forCellReuseIdentifier:@"HXFileTableViewCell"];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 54;
    _searchCon = [[UISearchController alloc] initWithSearchResultsController:nil];
    _searchCon.searchBar.delegate = self;
    [_searchCon.searchBar sizeToFit];
    _searchCon.dimsBackgroundDuringPresentation = NO;
    _searchCon.hidesNavigationBarDuringPresentation = NO;
    self.tableView.tableHeaderView = _searchCon.searchBar;
    
    //刷新控件
    self.tableView.mj_header=[AWRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    self.tableView.mj_footer=[AWRefreshFooder footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    self.tableView.mj_footer.automaticallyHidden = NO;
    [self.tableView.mj_header beginRefreshing];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self loadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    _searchCon.searchBar.text = @"";
    [self loadData];
}


-(void)loadData{
    self.page = 0;
    if (_searchCon.searchBar.text == nil) {
        _searchCon.searchBar.text = @"";
    }
    [NetworkAPI getFileListWithNum:[NSString stringWithFormat:@"%ld",(long)self.page]
                         QueryText:_searchCon.searchBar.text
                           Success:^(HXFileModel *result) {
                               if ([result.code isEqualToString:@"200"]) {
                                   [self.tableView.mj_footer resetNoMoreData];
                                   [SVProgressHUD showSuccessWithStatus:@"获取通知成功"];
                                   [self.tableView.mj_header endRefreshing];
                                   self.page++;
                                   [self.dataArray removeAllObjects];
                                   [self.dataArray addObjectsFromArray:result.resultlist];
                                   [self.tableView reloadData];
                                   if ([result.resultlist count] < 10) {
                                       [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                       self.tableView.mj_footer.alpha = 1.0f;
                                       return;
                                   }
                               }else if([result.code isEqualToString:@"400"]){
                                   [self.tableView.mj_header endRefreshing];
                                   [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                   self.tableView.mj_footer.alpha = 1.0f;
                               }else{
                                   [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                   [self.tableView.mj_header endRefreshing];
                               }
                           } failure:^(NSError *err) {
                               [self.tableView.mj_header endRefreshing];
                           }];
}

-(void)loadMoreData{
    if (_searchCon.searchBar.text == nil) {
        _searchCon.searchBar.text = @"";
    }
    [NetworkAPI getFileListWithNum:[NSString stringWithFormat:@"%ld",(long)self.page]
                         QueryText:_searchCon.searchBar.text
                           Success:^(HXFileModel *result) {
                               if ([result.code isEqualToString:@"200"]) {
                                   if ([result.resultlist count] == 0) {//如果没有数据了
                                       [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                       self.tableView.mj_footer.alpha = 1.0f;
                                       return;
                                   }else if([result.resultlist count] < 10){//有数据但是没有下一页了
                                       [self.dataArray addObjectsFromArray:result.resultlist];
                                       [self.tableView reloadData];
                                       [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                       self.tableView.mj_footer.alpha = 1.0f;
                                   }else{//还有更多数据
                                       self.page++;
                                       [self.dataArray addObjectsFromArray:result.resultlist];
                                       [self.tableView reloadData];
                                       [self.tableView.mj_footer endRefreshing];
                                       self.tableView.mj_footer.alpha = 0;
                                   }
                               }else if([result.code isEqualToString:@"400"]){
                                   [self.tableView.mj_header endRefreshing];
                                   [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                   self.tableView.mj_footer.alpha = 1.0f;
                               }else{
                                   [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                   [self.tableView.mj_footer endRefreshing];
                               }
                           } failure:^(NSError *err) {
                               [self.tableView.mj_footer endRefreshing];
                           }];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXFileTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXFileTableViewCell" forIndexPath:indexPath];
    if (indexPath.row % 2) cell.backgroundColor = MAINGRAYCOLOR;
    else cell.backgroundColor = [UIColor whiteColor];
    cell.data = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    _searchCon.searchBar.hidden = YES;
    _searchCon.searchBar.text = @"";
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HXFileDetailsViewController *vc = [[HXFileDetailsViewController alloc]init];
    HXFileListModel *data = self.dataArray[indexPath.row];
    vc.dataId = data.ID;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    _searchCon.searchBar.hidden = NO;
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    _searchCon.searchBar.hidden = YES;
}

#pragma mark - 懒加载
- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}





@end
